package com.llh.cheap.configs;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import top.moirae.cache.Cache;

@Configuration
@PropertySource("classpath:redis.properties")
public class RedisConfig {

    @Bean
    public Cache cache(
            @Value("${redis.host}") String host,
            @Value("${redis.port}") int port,
            @Value("${redis.password}") String password,
            @Value("${redis.time_out}") int timeOut
    ) {
        JedisPool pool = new JedisPool(new GenericObjectPoolConfig<>(), host, port, timeOut, StringUtils.isBlank(password) ? null : password, 0, "cheap");
        return new Cache(pool);
    }
}
